import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";
import _pullAllBy from "lodash/pullAllBy";
var _excluded = ["title", "onCancel", "onOk", "dataSource", "queryPermissionsByMenuId", "assignPermissions", "pagination", "visible", "form"];

var _dec, _dec2, _dec3, _dec4, _dec5, _dec6, _dec7, _dec8, _dec9, _dec10, _class, _class2;

import React, { PureComponent } from 'react';
import { Modal, Table, Form, Button, Input, Row, Col } from 'antd';
import { Bind } from 'lodash-decorators';
import { SEARCH_FORM_ITEM_LAYOUT } from 'utils/constants';
import intl from 'utils/intl';
var FormItem = Form.Item;
var viewMessagePrompt = 'ciam.menuConfig.view.message';
var modelPrompt = 'ciam.menuConfig.model.menuConfig';
var commonPrompt = 'cpaas.common';
var PermissionsModal = (_dec = Form.create({
  fieldNameProp: null
}), _dec2 = Bind(), _dec3 = Bind(), _dec4 = Bind(), _dec5 = Bind(), _dec6 = Bind(), _dec7 = Bind(), _dec8 = Bind(), _dec9 = Bind(), _dec10 = Bind(), _dec(_class = (_class2 = /*#__PURE__*/function (_PureComponent) {
  _inherits(PermissionsModal, _PureComponent);

  var _super = _createSuper(PermissionsModal);

  function PermissionsModal() {
    var _this;

    _classCallCheck(this, PermissionsModal);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));
    _this.state = {
      selectedRows: []
    };
    return _this;
  }

  _createClass(PermissionsModal, [{
    key: "onSelect",
    value: function onSelect(record, selected) {
      var _this$state$selectedR = this.state.selectedRows,
          selectedRows = _this$state$selectedR === void 0 ? [] : _this$state$selectedR;
      this.setState({
        selectedRows: selected ? selectedRows.concat(record) : selectedRows.filter(function (n) {
          return n.id !== record.id;
        })
      });
    }
  }, {
    key: "onSelectAll",
    value: function onSelectAll(selected, newSelectedRows, changeRows) {
      var _this$state$selectedR2 = this.state.selectedRows,
          selectedRows = _this$state$selectedR2 === void 0 ? [] : _this$state$selectedR2;
      this.setState({
        selectedRows: selected ? selectedRows.concat(changeRows) : _pullAllBy(_toConsumableArray(selectedRows), changeRows, 'id')
      });
    }
  }, {
    key: "ok",
    value: function ok() {
      var _this$props$onOk = this.props.onOk,
          onOk = _this$props$onOk === void 0 ? function (e) {
        return e;
      } : _this$props$onOk;
      var selectedRows = this.state.selectedRows;
      onOk(selectedRows);
      this.cancel();
    }
  }, {
    key: "cancel",
    value: function cancel() {
      this.setSelectedRows();
      this.reset();
    }
  }, {
    key: "onTableChange",
    value: function onTableChange() {
      var pagination = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      var _this$props = this.props,
          _this$props$handleFet = _this$props.handleFetchDataSource,
          handleFetchDataSource = _this$props$handleFet === void 0 ? function (e) {
        return e;
      } : _this$props$handleFet,
          _this$props$form$getF = _this$props.form.getFieldsValue,
          getFieldsValue = _this$props$form$getF === void 0 ? function (e) {
        return e;
      } : _this$props$form$getF;
      var _pagination$current = pagination.current,
          current = _pagination$current === void 0 ? 1 : _pagination$current,
          _pagination$pageSize = pagination.pageSize,
          pageSize = _pagination$pageSize === void 0 ? 10 : _pagination$pageSize;
      handleFetchDataSource(_objectSpread({
        page: current - 1,
        size: pageSize
      }, getFieldsValue()));
    }
  }, {
    key: "search",
    value: function search() {
      var _this$props2 = this.props,
          _this$props2$handleFe = _this$props2.handleFetchDataSource,
          handleFetchDataSource = _this$props2$handleFe === void 0 ? function (e) {
        return e;
      } : _this$props2$handleFe,
          _this$props2$form$get = _this$props2.form.getFieldsValue,
          getFieldsValue = _this$props2$form$get === void 0 ? function (e) {
        return e;
      } : _this$props2$form$get;
      handleFetchDataSource(_objectSpread({
        page: 0,
        size: 10
      }, getFieldsValue()));
    }
  }, {
    key: "reset",
    value: function reset() {
      var _this$props$form$rese = this.props.form.resetFields,
          resetFields = _this$props$form$rese === void 0 ? function (e) {
        return e;
      } : _this$props$form$rese;
      resetFields();
    }
  }, {
    key: "setSelectedRows",
    value: function setSelectedRows() {
      var selectedRows = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
      this.setState({
        selectedRows: selectedRows
      });
    }
  }, {
    key: "onCell",
    value: function onCell() {
      return {
        style: {
          overflow: 'hidden',
          maxWidth: 180,
          textOverflow: 'ellipsis',
          whiteSpace: 'nowrap'
        },
        onClick: function onClick(e) {
          var target = e.target;

          if (target.style.whiteSpace === 'normal') {
            target.style.whiteSpace = 'nowrap';
          } else {
            target.style.whiteSpace = 'normal';
          }
        }
      };
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props3 = this.props,
          title = _this$props3.title,
          onCancel = _this$props3.onCancel,
          onOk = _this$props3.onOk,
          dataSource = _this$props3.dataSource,
          queryPermissionsByMenuId = _this$props3.queryPermissionsByMenuId,
          assignPermissions = _this$props3.assignPermissions,
          pagination = _this$props3.pagination,
          visible = _this$props3.visible,
          _this$props3$form$get = _this$props3.form.getFieldDecorator,
          getFieldDecorator = _this$props3$form$get === void 0 ? function (e) {
        return e;
      } : _this$props3$form$get,
          others = _objectWithoutProperties(_this$props3, _excluded);

      var _this$state$selectedR3 = this.state.selectedRows,
          selectedRows = _this$state$selectedR3 === void 0 ? [] : _this$state$selectedR3;
      var tableProps = {
        columns: [{
          title: intl.get("".concat(modelPrompt, ".permissionCode")).d('权限编码'),
          dataIndex: 'code',
          width: 150,
          onCell: this.onCell
        }, {
          title: intl.get("".concat(modelPrompt, ".description")).d('描述'),
          dataIndex: 'description',
          width: 150,
          onCell: this.onCell
        }, {
          title: intl.get("".concat(modelPrompt, ".path")).d('路径'),
          dataIndex: 'path',
          width: 150,
          onCell: this.onCell
        }, {
          title: intl.get("".concat(modelPrompt, ".method")).d('方法'),
          dataIndex: 'method',
          width: 80
        }, {
          title: intl.get("".concat(modelPrompt, ".level")).d('层级'),
          dataIndex: 'levelMeaning',
          width: 80
        }],
        rowKey: 'id',
        rowSelection: {
          selectedRowKeys: selectedRows.map(function (n) {
            return n.id;
          }),
          onSelect: this.onSelect,
          onSelectAll: this.onSelectAll // fixed: true,

        },
        pagination: pagination,
        dataSource: dataSource,
        loading: queryPermissionsByMenuId,
        bordered: true,
        onChange: this.onTableChange
      };
      return /*#__PURE__*/React.createElement(Modal, Object.assign({
        title: title || intl.get("".concat(viewMessagePrompt, ".title.selectPermissions")).d('选择权限'),
        visible: visible,
        onOk: this.ok,
        onCancel: onCancel,
        width: 850,
        confirmLoading: assignPermissions,
        destroyOnClose: true
      }, others), /*#__PURE__*/React.createElement(Form, {
        style: {
          marginBottom: 15
        }
      }, /*#__PURE__*/React.createElement(Row, {
        type: "flex",
        align: "bottom",
        gutter: 24
      }, /*#__PURE__*/React.createElement(Col, {
        span: 8
      }, /*#__PURE__*/React.createElement(Form.Item, Object.assign({}, SEARCH_FORM_ITEM_LAYOUT, {
        label: intl.get("".concat(modelPrompt, ".catalogCode")).d('目录编码')
      }), getFieldDecorator('code')( /*#__PURE__*/React.createElement(Input, null)))), /*#__PURE__*/React.createElement(Col, {
        span: 8
      }, /*#__PURE__*/React.createElement(Form.Item, Object.assign({}, SEARCH_FORM_ITEM_LAYOUT, {
        label: intl.get("".concat(modelPrompt, ".conditionPermission")).d('描述/路径')
      }), getFieldDecorator('condition')( /*#__PURE__*/React.createElement(Input, null)))), /*#__PURE__*/React.createElement(Col, {
        span: 8
      }, /*#__PURE__*/React.createElement(FormItem, null, /*#__PURE__*/React.createElement(Button, {
        onClick: this.reset,
        style: {
          marginRight: 8
        }
      }, intl.get("".concat(commonPrompt, ".button.reset")).d('重置')), /*#__PURE__*/React.createElement(Button, {
        type: "primary",
        htmlType: "submit",
        onClick: this.search
      }, intl.get("".concat(commonPrompt, ".button.search")).d('查询')))))), /*#__PURE__*/React.createElement(Table, tableProps));
    }
  }]);

  return PermissionsModal;
}(PureComponent), (_applyDecoratedDescriptor(_class2.prototype, "onSelect", [_dec2], Object.getOwnPropertyDescriptor(_class2.prototype, "onSelect"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "onSelectAll", [_dec3], Object.getOwnPropertyDescriptor(_class2.prototype, "onSelectAll"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "ok", [_dec4], Object.getOwnPropertyDescriptor(_class2.prototype, "ok"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "cancel", [_dec5], Object.getOwnPropertyDescriptor(_class2.prototype, "cancel"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "onTableChange", [_dec6], Object.getOwnPropertyDescriptor(_class2.prototype, "onTableChange"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "search", [_dec7], Object.getOwnPropertyDescriptor(_class2.prototype, "search"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "reset", [_dec8], Object.getOwnPropertyDescriptor(_class2.prototype, "reset"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "setSelectedRows", [_dec9], Object.getOwnPropertyDescriptor(_class2.prototype, "setSelectedRows"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "onCell", [_dec10], Object.getOwnPropertyDescriptor(_class2.prototype, "onCell"), _class2.prototype)), _class2)) || _class);
export { PermissionsModal as default };